package com.hdax.dm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.common.code.CodeEnum;
import com.hdax.dm.common.exception.DmException;
import com.hdax.dm.entity.user.LinkUser;
import com.hdax.dm.user.dto.TicketBuyerDto;
import com.hdax.dm.user.mappers.ContactMapper;
import com.hdax.dm.user.service.ContactService;
import com.hdax.dm.utils.CommonResponse;
import com.hdax.dm.utils.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, LinkUser> implements ContactService {

    @Autowired
    private ContactService contactService;

    /**
     * 查询联系人
     * @param userId
     * @return
     */
    @Override
    public List<TicketBuyerDto> list(Long userId) {
        QueryWrapper<LinkUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        //查询全部
        List<LinkUser> list = list(queryWrapper);
        List<TicketBuyerDto> tickList = new ArrayList<TicketBuyerDto>();
        list.forEach(userLike->{
            TicketBuyerDto dto = new TicketBuyerDto();
            BeanUtils.copyProperties(userLike,dto);
            dto.setLinkId(userLike.getId());
            tickList.add(dto);
        });
        return tickList;
    }

    /**
     * 添加联系人
     * @param id
     * @param param
     * @return
     */
    @Override
    public CommonResponse<Integer> addContact(Long id, Map<String, String> param) {
        //添加
        super.save(LinkUser.builder()
                .userId(id)
                .name(param.get("name"))
                .idCard(param.get("idCard"))
                .cardType(Long.parseLong(param.get("cardType")))
                .build());
        return ResponseUtil.returnSuccess(null);
    }

    /**
     * 判断验票人是否存在
     * @param idCard
     * @return
     * @throws Exception
     */
    @Override
    public CommonResponse isNotContact(String idCard) throws Exception {

        QueryWrapper<LinkUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("idCard",idCard);
        if (this.getOne(queryWrapper)!=null){
            //已存在该常用联系人
            //抛出异常 代表的是验证码不正确
            throw new DmException(CodeEnum.USER_VCODE_INVALID);
        }else{
            //常用联系人不存在执行添加
            return ResponseUtil.returnSuccess(null);
        }
    }

    /**
     * 删除联系人
     * @param id
     * @return
     */
    @Override
    public CommonResponse delectContact(Long id) {
        QueryWrapper<LinkUser> queryWrapper = new QueryWrapper<LinkUser>();
        queryWrapper.eq("id",id);
        if (baseMapper.delete(queryWrapper)>0){
            return ResponseUtil.returnSuccess("null");
        }else{
            return ResponseUtil.returnFail("1010","删除失败");
        }
    }

    @Override
    public LinkUser eee(Long id, Long userId) {
        return contactService.eee(id,userId);
    }
}
